package BandB.Tool.GPhoneManager.Widget;

import BandB.Tool.GPhoneManager.ConfigManager;
import BandB.Tool.GPhoneManager.R;
import BandB.Tool.GPhoneManager.Toggles;
import BandB.Tool.GPhoneManager.AppManager.Settings;
import BandB.Tool.GPhoneManager.AppManager.UninstallerList;
import BandB.Tool.GPhoneManager.AppManager.Grid.UninstallerGrid;
import BandB.Tool.GPhoneManager.FileExplorer.FileExplorer;
import BandB.Tool.GPhoneManager.TaskManager.ProcessGrid;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

public class GPhoneWidget extends AppWidgetProvider implements
		AirplaneListener, WifiListener {

	private static final String TAG = GPhoneWidget.class.getSimpleName();
	public static final String ACTION_SILENT_CHANGED = "silent_mode_changed";

	// The following two variables customized will be used to determine whether
	// change mode(airplane, silent, wi_fi) forced

	@Override
	public void onAirplaneModeChanged(int turn, RemoteViews views) {
		// TODO Auto-generated method stub
		if (turn == AIRPLANE_TURN_ON) {
			views.setImageViewResource(R.id.btnAirplane, R.drawable.airplane3);
		} else {
			views.setImageViewResource(R.id.btnAirplane, R.drawable.airplane);
		}
	}

	@Override
	public void onWifiStatusChanged(int turn, RemoteViews views) {
		// TODO Auto-generated method stub
		if (turn == WIFI_TURN_ON) {
			views.setImageViewResource(R.id.btnWifi, R.drawable.wifi3);
		} else {
			views.setImageViewResource(R.id.btnWifi, R.drawable.wifi);
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		Log.i(TAG, "GPhoneWidget onUpdate called.");
		final int[] Ids = AppWidgetManager
				.getInstance(context)
				.getAppWidgetIds(new ComponentName(context, GPhoneWidget.class));

		final RemoteViews views = generateRemoveViews(context);
		initializeViews(context, views);
		appWidgetManager.updateAppWidget(Ids, views);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.widget_title);

		String action = intent.getAction();
		Log.i(TAG, "received broadcast:" + action);

		if (action.equals(Settings.ACTION_UPDATE_WIDGET)) {
			Log.i(TAG, "process broadcast:" + Settings.ACTION_UPDATE_WIDGET);
			processWidgetSettings(context, intent, views);
		}  
	}

	private RemoteViews generateRemoveViews(Context c) {
		RemoteViews views = new RemoteViews(c.getPackageName(),
				R.layout.widget_title);
		Intent _fileIntent = new Intent(c, FileExplorer.class);
		PendingIntent filePendingIntent = PendingIntent.getActivity(c, 0,
				_fileIntent, 0);
		Intent _appListIntent = new Intent(c, UninstallerList.class);
		PendingIntent appListPendingIntent = PendingIntent.getActivity(c, 0,
				_appListIntent, 0);
		Intent _appGridIntent = new Intent(c, UninstallerGrid.class);
		PendingIntent appGridPendingIntent = PendingIntent.getActivity(c, 0,
				_appGridIntent, 0);
		Intent _taskListIntent = new Intent(c, ProcessGrid.class);
		PendingIntent taskListPendingIntent = PendingIntent.getActivity(c, 0,
				_taskListIntent, 0);
		Intent _taskGridIntent = new Intent(c, Toggles.class);
		PendingIntent taskGridPendingIntent = PendingIntent.getActivity(c, 0,
				_taskGridIntent, 0);


		views.setOnClickPendingIntent(R.id.btnFile, filePendingIntent);
		views.setOnClickPendingIntent(R.id.btnAppList, appListPendingIntent);
		views.setOnClickPendingIntent(R.id.btnAppGrid, appGridPendingIntent);
		views.setOnClickPendingIntent(R.id.btnTaskList, taskListPendingIntent);
		views.setOnClickPendingIntent(R.id.btnTaskGrid, taskGridPendingIntent);

		return views;
	}

	private void initializeViews(Context c, RemoteViews views) {
		ConfigManager manager = ConfigManager.getInstance(c);
		String[] configs = manager.getAllInfos();
		if (!Boolean.valueOf(configs[ConfigManager.FILE_INDEX])) {
			views.setViewVisibility(R.id.btnFile, View.GONE);
		}
		if (!Boolean.valueOf(configs[ConfigManager.APP_LIST_INDEX])) {
			views.setViewVisibility(R.id.btnAppList, View.GONE);
		}
		if (!Boolean.valueOf(configs[ConfigManager.APP_GRID_INDEX])) {
			views.setViewVisibility(R.id.btnAppGrid, View.GONE);
		}
		if (!Boolean.valueOf(configs[ConfigManager.TASK_LIST_INDEX])) {
			views.setViewVisibility(R.id.btnTaskList, View.GONE);
		}
		if (!Boolean.valueOf(configs[ConfigManager.TASK_GRID_INDEX])) {
			views.setViewVisibility(R.id.btnTaskGrid, View.GONE);
		}


	}

	/**
	 * If receive events of user customized broadcast, handle it.
	 * 
	 * @param c
	 *            a {@link Context} object
	 * @param intent
	 *            a Intent object which contains data
	 * @param views
	 *            a {@link RemoteViews} object to update
	 */
	private void processWidgetSettings(Context c, Intent intent,
			RemoteViews views) {
		views.setViewVisibility(R.id.btnFile, intent.getIntExtra(
				Settings.FILE_STATUS, 0));
		views.setViewVisibility(R.id.btnAppList, intent.getIntExtra(
				Settings.APP_LIST_STATUS, 0));
		views.setViewVisibility(R.id.btnAppGrid, intent.getIntExtra(
				Settings.APP_LIST_STATUS, 0));
		views.setViewVisibility(R.id.btnTaskList, intent.getIntExtra(
				Settings.TASK_LIST_STATUS, 0));
		views.setViewVisibility(R.id.btnTaskGrid, intent.getIntExtra(
				Settings.TASK_GRID_STATUS, 0));
		AppWidgetManager m = AppWidgetManager.getInstance(c);
		m.updateAppWidget(new ComponentName(c, GPhoneWidget.class), views);
	}

}
